package com.oxbix.spanlogistics.wxapi;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.kye.express.R;
import com.kye.express.view.ToastManager;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXEntryActivity extends AppCompatActivity implements IWXAPIEventHandler{

    private IWXAPI mWXApi;
    private static final String TENCENT_WECHAT_APPID = "wxb3c0dc14850a5358";
    private int flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wxentry);
        if (mWXApi == null){
            mWXApi = WXAPIFactory.createWXAPI(this, TENCENT_WECHAT_APPID, true);
            mWXApi.registerApp(TENCENT_WECHAT_APPID);
            mWXApi.handleIntent(getIntent(), this);
        }
    }

    @Override
    public void onReq(BaseReq baseReq) {
        SendMessageToWX.Req req = (SendMessageToWX.Req) baseReq;
        flag = req.scene;
    }

    @Override
    public void onResp(BaseResp baseResp) {
        int result = 0;
        int type = baseResp.getType();

        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                result = R.string.errcode_success;
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                result = R.string.errcode_cancel;
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                result = R.string.errcode_deny;
                break;
            default:
                result = R.string.errcode_unknown;
                break;
        }
        if (flag == 0){
            ToastManager.show(this,"微信分享 "+getResources().getString(result),Toast.LENGTH_SHORT);
        }else {
            ToastManager.show(this,"微信朋友圈分享 "+getResources().getString(result),Toast.LENGTH_SHORT);
        }
        finish();
    }
}
